package com.banzhi.shop.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.banzhi.shop.R;
import com.banzhi.shop.adapter.CommonAdapter;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.entity.Wuliu.MyWuLiu_Data;
import com.banzhi.shop.entity.Wuliu.WuLiu_Data;
import com.banzhi.shop.model.CallBack.WuLiuCallBack;
import com.banzhi.shop.model.IWuLiuModel;
import com.banzhi.shop.model.Impl.WuliuImpl;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 填写退货的物流信息
 */
public class ShouhouWuliuActivity extends BaseActivity implements View.OnClickListener, WuLiuCallBack {
    private ImageView ivback;
    private IWuLiuModel model;
    /**
     * 快递公司
     */
    private TextView tvkuaidi;
    private List<String> wuLiu = new ArrayList<>();
    private Button btnsave;
    private EditText etkuaidi;
    private List<WuLiu_Data> datas;
    private String pingyin;
    private String refundid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shouhou_wuliu);
        Translucent.setTranslucent(this);
        initViews();
        setListeners();
        model = new WuliuImpl();
        Intent intent = getIntent();
        refundid = intent.getStringExtra("refundid");
        LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
        model.getWuliu(refundid, this);
    }

    private void initViews() {
        ivback = (ImageView) findViewById(R.id.iv_back);
        tvkuaidi = (TextView) findViewById(R.id.tv_kuaidi);
        btnsave = (Button) findViewById(R.id.btn_save);
        etkuaidi = (EditText) findViewById(R.id.et_kuaidi);
    }

    private void setListeners() {
        ivback.setOnClickListener(this);
        tvkuaidi.setOnClickListener(this);
        btnsave.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_kuaidi:
                LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
                model.getWuliu(this);
                break;
            case R.id.btn_save:
                if (!ASUtils.isCanUse(tvkuaidi.getText().toString().trim())) {
                    ToastUtils.makeToastShort("请先选择快递公司");
                } else if (!ASUtils.isCanUse(etkuaidi.getText().toString().trim())) {
                    ToastUtils.makeToastShort("请先输入快读单号");
                } else {
                    for (int i = 0; i < wuLiu.size(); i++) {
                        if (tvkuaidi.getText().toString().trim().equals(datas.get(i).getName())) {
                            pingyin = datas.get(i).getExpress();
                        }
                    }
                    LoadingDialogUtils.showProgress(this, "正在加载,请稍后~");
                    model.save(refundid, pingyin, tvkuaidi.getText().toString().trim(), etkuaidi.getText().toString().trim(), this);
                }
                break;
        }
    }

    /**
     * 获取物流信息成功
     *
     * @param object
     */
    @Override
    public void ongetWuLiuSuccess(Object object) {
        datas = (List<WuLiu_Data>) object;
        wuLiu.clear();
        for (int i = 0; i < datas.size(); i++) {
            wuLiu.add(datas.get(i).getName());
        }
        showTypePopupwindow(wuLiu, tvkuaidi, tvkuaidi);
    }

    /**
     * 获取物流信息失败
     *
     * @param object
     */
    @Override
    public void ongetWuLiuError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }

    /**
     * 获取我填写的物流信息成功
     *
     * @param object
     */
    @Override
    public void ongetDataSuccess(Object object) {
        MyWuLiu_Data myWuLiu_data = (MyWuLiu_Data) object;
        if (ASUtils.isCanUse(myWuLiu_data.getExpresscom())) {
            tvkuaidi.setText(myWuLiu_data.getExpresscom());
        }
        if (ASUtils.isCanUse(myWuLiu_data.getExpresssn())) {
            etkuaidi.setText(myWuLiu_data.getExpresssn());
        }
    }

    /**
     * 获取我填写的物流信息失败
     *
     * @param object
     */
    @Override
    public void ongetDataError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }

    /**
     * 提交快读信息成功
     *
     * @param object
     */
    @Override
    public void saveSuccess(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
        finish();
    }

    /**
     * 提交失败
     *
     * @param object
     */
    @Override
    public void saveError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        NoTokenUtils.loginOut(this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }

    private void showTypePopupwindow(final List<String> list, View view, final TextView tv) {
        //设置contentView
        View contentView = LayoutInflater.from(this).inflate(R.layout.popupwindow_choice_kuaidi, null);
        final PopupWindow popWindow = new PopupWindow(contentView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        popWindow.setContentView(contentView);

        final RecyclerView recyclerView = (RecyclerView) contentView.findViewById(R.id.recyclerview);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        CommonAdapter<String> adapter = null;
        if (adapter == null) {
            adapter = new CommonAdapter<String>(ShouhouWuliuActivity.this, R.layout.item_refund_request, list) {
                @Override
                public void convert(ViewHolder holder, String s, int position) {
                    holder.setText(R.id.tv_text, (String) s);
                }
            };
        } else {
            adapter.notifyDataSetChanged();
        }
        adapter.setOnItemClickListener(new CommonAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                tv.setText((String) list.get(position));
                popWindow.dismiss();
            }
        });
        recyclerView.setAdapter(adapter);
        popWindow.setFocusable(true);
        popWindow.setOutsideTouchable(true);
        popWindow.setBackgroundDrawable(new BitmapDrawable());
        popWindow.showAsDropDown(view, 0, 0);
        popWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                setAlpha(1.0f, ShouhouWuliuActivity.this);
            }
        });
        setAlpha(0.8f, ShouhouWuliuActivity.this);
    }

    public void setAlpha(float alpha, Activity activity) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = alpha;
        activity.getWindow().setAttributes(lp);
    }

}
